package com.zking.web;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.SysUser;
import com.zking.service.UserService;
import com.zking.utils.RedisUtil;
import com.zking.utils.RequestUtils;


@Controller
@RequestMapping(value = "/music")
public class MusicInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;
    
    @RequestMapping("/login")
    @RequiresPermissions({"/usersPage"})
    @ResponseBody
    public String login(){
        SysUser user = RequestUtils.currentLoginUser();
        Set<String> authorization = userService.findPermissionsByUserId(user.getId());
        return "该用户有如下权限" + authorization;
    }
    
    //简单的测试
    @RequestMapping("/redisUtil")
    @ResponseBody
    public String redisUtilTest() throws InterruptedException{
    	redisUtil.set("aaaKey", "aaaValue");
        redisUtil.expire("aaaKey", 10);
        Object aaaKey1 = redisUtil.get("aaaKey");
        TimeUnit.SECONDS.sleep(11);
        Object aaaKey2 = redisUtil.get("aaaKey");
        return "aaaKey1"+aaaKey1+",aaaKey2="+aaaKey2;
    }
    
    /**
     * 	在Redis中设置实例对象
     */
    @RequestMapping("/setUserObj")
    @ResponseBody
    public String setUserObj(SysUser user) {
    	//先存储部分信息，比如，用户名
    	//将对象转化为json
    	//类似于：{"username":"admin"}
    	redisUtil.set("user", JSONObject.toJSONString(user));
    	return redisUtil.get("user").toString();
    }
    
    /**
     * 	设置第二次
     */
    @RequestMapping("/setUserObj2")
    @ResponseBody
    //user:传过来的参数
    public String setUserObj2(SysUser user) {
    	//将原来存储的User的json数据取出来，再往里面存储一些数据
    	//{"userName":"super"}
    	SysUser redisUser = JSONObject.parseObject(redisUtil.get("user").toString(), SysUser.class);
    	//又新加了密码和是否加锁两个数据
    	redisUser.setPassWord(user.getPassWord());
    	redisUser.setUserEnable(user.getUserEnable());
    	//将原来的user进行覆盖
    	redisUtil.set("user", JSONObject.toJSONString(redisUser));
    	return redisUtil.get("user").toString();
    }

    @RequestMapping("/getUserObj")
    @ResponseBody
    public String getUserObj() {
    	//获取到第一步和第二步得到的整个User对象
    	String redisUserStr = redisUtil.get("user").toString();
    	//"{\"passWord\":\"1266666\",\"userEnable\":1,\"userName\":\"super\"}"
    	System.out.println(JSONObject.parseObject(redisUserStr, SysUser.class));
    	return redisUserStr;
    }
    
    /**
     * 	没有用之后直接删除缓存
     */
    @RequestMapping("/clearUserObj")
    @ResponseBody
    public String clearUserObj() {
    	String redisUserStr = redisUtil.get("user").toString();
    	SysUser user = JSONObject.parseObject(redisUserStr, SysUser.class);
    	//user一次性存储到数据库
    	//删除redis缓存
    	redisUtil.del("user");
    	return "清除成功！";
    }
    
//    public static void main(String[] args) {
//    	SysUser user = new SysUser();
//    	user.setId(1);
//    	user.setUserName("admin");
//    	user.setPassWord("123456");
//		System.out.println(JSONObject.toJSONString(user));
//	}
    
}
